<%--
  - Author		:	이 영호
  --%>
<%@ page language="java" %>
<%@ page contentType="text/html; charset=utf-8" %>
<%@ page pageEncoding="utf-8" %>
<%@ include file="/WEB-INF/pages/includes/taglibs.jspf" %>
<%@ include file="/WEB-INF/pages/includes/variables.jspf" %>
<%@ include file="/WEB-INF/pages/includes/doctype.jspf" %>
<html>
	<head>
<%@ include file="/WEB-INF/pages/includes/metatags.jspf" %>
		<title><fmt:message key="pages.title" /></title>
		<link rel="stylesheet" type="text/css" href="<c:url value="${dirStyles}/common.css" />" />

		<script type="text/javascript" src="<c:url value="${dirScripts}/jquery-1.7.2.min.js" />"></script>
		<script type="text/javascript">
			$(function(){
			});
			
			function validation(){
				try{
					with(document.frm){
						bizNum.value=bizNum1.value+"-"+bizNum2.value+"-"+bizNum3.value;
						tel.value=tel1.value+"-"+tel2.value+"-"+tel3.value;
						mobile.value=mobile1.value+"-"+mobile2.value+"-"+mobile3.value;
						fax.value=fax1.value+"-"+fax2.value+"-"+fax3.value;
						addrNum.value=addrNum1.value+"-"+addrNum2.value;
						
						var obj;
						for(var i=0;i<$("input").length;i++){
							obj = $("input").eq(i); 
							if($(obj).attr("type") != "hidden" && $(obj).attr("name") && $(obj).val().replace(/\s/g, '') == ""){
								alert($(obj).attr("title")+"을(를) 입력 해주세요.");
								$(obj).focus();
								return false;
							}
						}
						
						if(id){
							if(eval(chkDuplId())){
								alert("아이디가 중복되었습니다.");
								id.focus();	
								return false;
							}
							
							if(id.value.replace(/\s/g, '') == ""){
								return false;
							}
						}
							
						if(bizNum1.value.replace(/\s/g, '') == ""){
							alert("사업자 등록 번호를 입력하세요.");
							return false;
						}
						if(bizNum2.value.replace(/\s/g, '') == ""){
							alert("사업자 등록 번호를 입력하세요.");
							return false;
						}
						if(bizNum3.value.replace(/\s/g, '') == ""){
							alert("사업자 등록 번호를 입력하세요.");
							return false;
						}
						
						if(pw.value.replace(/\s/g, '') == ""){
							alert("비밀번호를 입력하세요.");
							return false;
						}
						if(pwChk.value.replace(/\s/g, '') == ""){
							alert("비밀번호 확인을 입력하세요.");
							return false;
						}
						
						if(pw.value != pwChk.value){
							alert("비밀번호와 비밀번호 확인의 값이 같지 않습니다.");
							pw.focus();
							return false;
						}
						
					}
				}catch(err){
					alert(err);
					return false;	
				}
				
				if(! confirm("회원을 <c:if test="${empty user}">등록</c:if><c:if test="${not empty user}">수정</c:if> 하시겠습니까?")){
					return false;
				}
				
				return true;
			}
			
			function chkId(){
				if(eval(chkDuplId())){
					alert("아이디가 중복되었습니다.");
					document.frm.id.focus();	
				}else{
					alert("사용가능한 아이디입니다.");
				}
			}
			
			function chkDuplId(){
				var result = false;
				
				var idx =  "";
				if(document.frm.idx){
					idx =  document.frm.idx.value;
				}

				var userId =  document.frm.id.value;
				if(userId.replace(/\s/g, '') == ""){
					alert("아이디를 입력하세요.");
					document.frm.id.focus();	
				}else{
					$.ajax({
						type: "POST",
						url: "<c:url value="/backoffice/user/chkDupl.do" />",
						dataType: "text",
						async: false,
						data: {
							"idx" : idx
							, "userId" : userId
						},
						success: function(data){
							result = data;
						},
						beforeSend: function(){
						},
						error: function(jqXHR, textStatus, errorThrown){
							//alert(jqXHR.responseText); //에러 페이지 내용
							alert("오류가 발생하였습니다.\n관리자에게 문의 바랍니다.");
						},
						complete: function(){
						}
					 });
				}
				return result;
			}
			
			function addrPop(){
				window.open("<c:url value="/addr.jsp" />", "addr", "channelmode=0,directories=0,fullscreen=0,width=400,height=300");
			}
			
			function setAddr(addrNum, addr1){
				document.frm.addrNum1.value = addrNum.split("-")[0]; 
				document.frm.addrNum2.value = addrNum.split("-")[1];
				document.frm.addr1.value = addr1;
			}

		</script>
	</head>
	<body>
<%@ include file="/WEB-INF/pages/includes/header.jsp" %>
		<div class="bodyWrap">
<%@ include file="/WEB-INF/pages/includes/left.jsp" %>
			<div class="contWrap">
				<div class="contMenuTitle">
					<img src="<c:url value="${dirImages}/subtit_07_1.gif" />" title="회원관리" alt="회원관리">
				</div>
				<div class="secTitle">
					<img src="<c:url value="${dirImages}/icon.png" />" title="회원관리" alt="회원관리" > 회원관리
				</div>
				<form name="frm" method="post" action="<c:url value="/backoffice/user/submit.do" />" onsubmit="return validation();">
					<table class="formTbl">
						<colgroup>
							<col width="140px" />
							<col width="*" />
						</colgroup>
						<caption style="color: #c9c9c9; font-size: 12px;"><font class="redFont">*</font>표시는 필수입력 사항</caption>
						<tbody>
							<tr>
								<th><font class="redFont">*</font> 아이디</th>
								<td>
									<c:choose>
										<c:when test="${not empty user}">
											${user.id}
										</c:when>
										<c:otherwise>
											<input type="text" name="id" title="아이디" alt="아이디" maxlength="15" value="${user.id}">
										</c:otherwise>
									</c:choose>
								</td>
								<c:choose>
									<c:when test="${not empty user}">
										<th>회원상태</th>
										<td>
											<select name="enabled">
												<option value="0" <c:if test="${user.enabled eq 0}"> selected="selected" </c:if>>가입대기</option>
												<option value="1" <c:if test="${user.enabled eq 1}"> selected="selected" </c:if>>정회원</option>
											</select>
										</td>
									</c:when>
									<c:otherwise>
										<th>아이디 중복확인</th>
										<td>
											<a href="javascript:chkId();"><img class="btn" src="<c:url value="${dirImages}/join_id_check.gif" />" title="아이디 중복확인" alt="아이디 중복확인" /></a>
										</td>
									</c:otherwise>
								</c:choose>
							</tr>
							<tr>
								<th><font class="redFont">*</font> 사업자 등록 번호</th>
								<td colspan="3">
									<input type="text" name="bizNum1" title="사업자 등록 번호" alt="사업자 등록 번호" value="${fn:split(user.bizNum, '-')[0]}">-<input type="text" name="bizNum2" title="사업자 등록 번호" alt="사업자 등록 번호" value="${fn:split(user.bizNum, '-')[1]}">-<input type="text" name="bizNum3" title="사업자 등록 번호" alt="사업자 등록 번호" value="${fn:split(user.bizNum, '-')[2]}">
									<input type="hidden" name="bizNum" title="사업자 등록 번호" alt="사업자 등록 번호" value="${user.bizNum}">
								</td>
							</tr>
							<tr>
								<th><font class="redFont">*</font> 이름</th>
								<td colspan="3">
									<input type="text" name="name" title="이름" alt="이름" value="${user.name}">
								</td>
							</tr>
							<tr>
								<th><font class="redFont">*</font> 비밀번호</th>
								<td>
									<input type="password" name="pw" title="비밀번호" alt="비밀번호" maxlength="15" value="${user.pw}">
								</td>
								<th><font class="redFont">*</font> 비밀번호 확인</th>
								<td>
									<input type="password" name="pwChk" title="비밀번호 확인" alt="비밀번호 확인" maxlength="15" value="${user.pw}">
								</td>
							</tr>
							<tr>
								<th><font class="redFont">*</font> 이메일</th>
								<td colspan="3">
									<input type="text" name="email" title="이메일" alt="이메일" maxlength="30" value="${user.email}">
								</td>
							</tr>
							<tr>
								<th><font class="redFont">*</font> 업체명</th>
								<td colspan="3">
									<input type="text" name="bizNm" title="업체명" alt="업체명"  maxlength="25" value="${user.bizNm}">
								</td>
							</tr>
							<tr>
								<th><font class="redFont">*</font> 부서명</th>
								<td colspan="3">
									<input type="text" name="team" title="부서명" alt="부서명" maxlength="25" value="${user.team}">
								</td>
							</tr>
							<tr>
								<th><font class="redFont">*</font> 회원등급</th>
								<td colspan="3">
									<select name="authority">
										<option value="ROLE_ADMIN" <c:if test="${user.authority eq 'ROLE_ADMIN'}"> selected="selected" </c:if>>운영자</option>
										<option value="ROLE_USER" <c:if test="${user.authority eq 'ROLE_USER'}"> selected="selected" </c:if>>일반회원</option>
										<option value="ROLE_TESTER" <c:if test="${user.authority eq 'ROLE_TESTER'}"> selected="selected" </c:if>>체험단</option>
									</select>
								</td>
							</tr>
							<tr>
								<th><font class="redFont">*</font> 전화번호</th>
								<td colspan="3">
									<input type="text" name="tel1" title="전화번호" alt="전화번호" maxlength="3" size="3" value="${fn:split(user.tel, '-')[0]}">
									-
									<input type="text" name="tel2" title="전화번호" alt="전화번호" maxlength="4" size="4" value="${fn:split(user.tel, '-')[1]}">
									-
									<input type="text" name="tel3" title="전화번호" alt="전화번호" maxlength="4" size="4" value="${fn:split(user.tel, '-')[2]}">
									<input type="hidden" name="tel" title="전화번호" alt="전화번호" value="${user.tel}">
								</td>
							</tr>
							<tr>
								<th><font class="redFont">*</font> 휴대전화</th>
								<td colspan="3">
									<input type="text" name="mobile1" title="휴대전화" alt="휴대전화" maxlength="3" size="3" value="${fn:split(user.mobile, '-')[0]}">
									-
									<input type="text" name="mobile2" title="휴대전화" alt="휴대전화" maxlength="4" size="4" value="${fn:split(user.mobile, '-')[1]}">
									-
									<input type="text" name="mobile3" title="휴대전화" alt="휴대전화" maxlength="4" size="4" value="${fn:split(user.mobile, '-')[2]}">
									<input type="hidden" name="mobile" title="휴대전화" alt="휴대전화" value="${user.mobile}">
								</td>
							</tr>
							<tr>
								<th><font class="redFont">*</font> FAX</th>
								<td colspan="3">
									<input type="text" name="fax1" title="FAX" alt="FAX" maxlength="3" size="3" value="${fn:split(user.fax, '-')[0]}">
									-
									<input type="text" name="fax2" title="FAX" alt="FAX" maxlength="4" size="4" value="${fn:split(user.fax, '-')[1]}">
									-
									<input type="text" name="fax3" title="FAX" alt="FAX" maxlength="4" size="4" value="${fn:split(user.fax, '-')[2]}">
									<input type="hidden" name="fax" title="FAX" alt="FAX" value="${user.fax}">
								</td>
							</tr>
							<tr>
								<th><font class="redFont">*</font> 우편번호</th>
								<td colspan="3">
									<input type="text" name="addrNum1" title="우편번호" alt="우편번호" maxlength="3" size="3" readonly="readonly" onfocus="javascript:this.blur();" value="${fn:split(user.addrNum, '-')[0]}">
									-
									<input type="text" name="addrNum2" title="우편번호" alt="우편번호" maxlength="3" size="3" readonly="readonly" onfocus="javascript:this.blur();" value="${fn:split(user.addrNum, '-')[1]}">
									<img class="btn" src="<c:url value="${dirImages}/join_add_check.gif" />" title="우편번호 확인" alt="우편번호 확인" style="vertical-align: top;" onclick="javascript:addrPop();" />
									<input type="hidden" name="addrNum" title="우편번호" alt="우편번호" value="${user.addrNum}">
								</td>
							</tr>
							<tr>
								<th><font class="redFont">*</font> 주소1</th>
								<td colspan="3">
									<input type="text" name="addr1" title="주소1" alt="주소1" maxlength="80" size="80" value="${user.addr1}">
								</td>
							</tr>
							<tr>
								<th><font class="redFont">*</font> 주소2</th>
								<td colspan="3">
									<input type="text" name="addr2" title="주소2" alt="주소2" maxlength="80" size="80" value="${user.addr2}">
								</td>
							</tr>
						</tbody>
					</table>
					<div class="btnWrap centerBtnWrap">
						<c:choose>
							<c:when test="${param.fAct eq 'modify'}">
								<input type="hidden" name="idx" value="${user.idx}">
								<input type="hidden" name="fAct" value="modify">
								<input type="submit" class="btn btn_revision" alt="수정" title="수정" value="">
							</c:when>
							<c:otherwise>
								<input type="submit" class="btn btn_register" alt="등록" title="등록" value="">
							</c:otherwise>
						</c:choose>
						<input type="button" class="btn btn_cancel" alt="취소" title="취소" onclick="javascript:document.listFrm.submit();">
					</div>
				</form>
			</div>
		</div>
		<form name="listFrm" method="post" action="<c:url value="/backoffice/user/list.do" />">
			<input type="hidden" name="maxnum" value="${param.maxnum}" />
			<input type="hidden" name="page" value="${param.page}" />
			<input type="hidden" name="searchType" value="${param.searchType}" />
			<input type="hidden" name="searchWord" value="${param.searchWord}" />
		</form>
		<%@ include file="/WEB-INF/pages/includes/footer.jsp" %>
	</body>
</html>